#include "attribute.h"
#include <sstream>
#include <stdlib.h>

//-------------------------------------------------------------------------
Attribute::Attribute( string _name, AttribPrototype* _prototype )
: m_prototype( _prototype )
, m_name( _name )
{
  vector<string> propertyNames = _prototype->getPropertyNames();
  vector<string> propertyTypes = _prototype->getPropertyTypes();
  for( int i=0; i<propertyTypes.size(); i++ )
  {
    string pName = propertyNames[i];
    if( propertyTypes[i] == "BOOLEAN" )
    {
      m_propBool[pName] = true;
    }
    else if( propertyTypes[i] == "INTEGER" )
    {
      m_propInteger[pName] = 0;
    }
    else if( propertyTypes[i] == "REAL" )
    {
      m_propReal[pName] = 0.0;
    }
    else if( propertyTypes[i] == "STRING" )
    {
      m_propString[pName] = "";
    }
  }
}
//-------------------------------------------------------------------------



//-------------------------------------------------------------------------
string 
Attribute::getName()
{
  return m_name;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
string 
Attribute::getPrototypeName()
{
  return m_prototype->getName();
}
//-------------------------------------------------------------------------



//-------------------------------------------------------------------------
map<string,string> 
Attribute::getProperties()
{
  map<string,string> prop;

  map<string,bool>::iterator itB;
  for( itB=m_propBool.begin(); itB!=m_propBool.end(); itB++ )
  {
    if( itB->second )
      prop[itB->first] = "true";
    else
      prop[itB->first] = "false";
  }
  map<string,int>::iterator itI;
  for( itI=m_propInteger.begin(); itI!=m_propInteger.end(); itI++ )
  {
    std::ostringstream ss;
    ss << itI->second;
    prop[itI->first] = ss.str();
  }
  map<string,double>::iterator itR;
  for( itR=m_propReal.begin(); itR!=m_propReal.end(); itR++ )
  {
    std::ostringstream ss;
    ss << itR->second;
    prop[itR->first] = ss.str();
  }
  map<string,string>::iterator itS;
  for( itS=m_propString.begin(); itS!=m_propString.end(); itS++ )
    prop[itS->first] = itS->second;

  return prop;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
bool  
Attribute::setPropertyValue( string _prop, bool _value )
{
  map<string,bool>::iterator it;
  it = m_propBool.find( _prop );
  if( it != m_propBool.end() )
  {
    it->second = _value;
    return true;
  }
  return false;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
bool  
Attribute::setPropertyValue( string _prop, int _value )
{
  map<string,int>::iterator it;
  it = m_propInteger.find( _prop );
  if( it != m_propInteger.end() )
  {
    it->second = _value;
    return true;
  }
  return false;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
bool  
Attribute::setPropertyValue( string _prop, double _value )
{
  map<string,double>::iterator it;
  it = m_propReal.find( _prop );
  if( it != m_propReal.end() )
  {
    it->second = _value;
    return true;
  }
  return false;
}
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
bool  
Attribute::setPropertyValue( string _prop, string _value )
{
  map<string,string>::iterator it;
  it = m_propString.find( _prop );
  if( it != m_propString.end() )
  {
    it->second = _value;
    return true;
  }
  return false;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
bool  
Attribute::setPropertyValues( string _prop, string _value )
{
  map<string,bool>::iterator itB = m_propBool.find( _prop );
  if( itB != m_propBool.end() )
  {
    if( _value == "true" )
      itB->second = true;
    else
      itB->second = false;
    return true;
  }
  map<string,int>::iterator itI = m_propInteger.find( _prop );
  if( itI != m_propInteger.end() )
  {
    itI->second = atoi( _value.c_str() );
    return true;
  }
  map<string,double>::iterator itR = m_propReal.find( _prop );
  if( itR != m_propReal.end() )
  {
    itR->second = atof( _value.c_str() );
    return true;
  }
  map<string,string>::iterator itS = m_propString.find( _prop );
  if( itS != m_propString.end() )
  {
    itS->second = _value;
    return true;
  }
  return false;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
vector<int> 
Attribute::getSymbol( string _prop ) 
{ 
  string type   = m_prototype->getPropertyType( _prop );
  int direction = m_prototype->getPropertySymb( _prop );

  if( type == "BOOLEAN" )
  {
    if( m_propBool[_prop] == true )
    {
      return m_prototype->getSymbol( direction, 0 ); 
    }
  }
  else if( type == "REAL" )
  {
    if( m_propReal[_prop] < 0 )
    {
      return m_prototype->getSymbol( direction, 0 ); 
    }
    else if( m_propReal[_prop] > 0 )
    {
      return m_prototype->getSymbol( direction, 1 ); 
    }
  }

  return m_prototype->getSymbol( 0, 0 ); 
};
//-------------------------------------------------------------------------

